// pages/self- check-confirm/self- check-confirm.js
import { selfDetectInfo } from '../../services/inspect'
import { checkIfLogin } from '../../utils/check'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    vehicleInfo:{
      title:'选择车辆信息',
      label1: '车牌:',
      value1: "",
      label2: "车型:",
      value2: "",
      url:'/pages/select-vehicle/select-vehicle'
    },
    submitInfo:{},

    showDialogAlert: false,
    dialogMsg:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    console.log("query>>>", query)
    if(!(query && query.q)){
      return
    }
    // checkIfLogin()
    const q = decodeURIComponent(query.q) // 获取到二维码原始链接内容
    const scancode_time = parseInt(query.scancode_time) // 获取用户扫码时间 UNIX 时间戳
    console.log("q>>>", q)
    console.log("scancode_time>>>", scancode_time)
    if(q){
      const qList = q.split('/')
      let spaceId = qList[qList.length - 1]
      console.log("spaceId>>>", spaceId)
      wx.setStorageSync('spaceId', spaceId)
      wx.setStorageSync('queryFromScan', true)
    }
  },

  onShow(){
    checkIfLogin()
    let vehicleId = wx.getStorageSync('vehicleId')
    let plateNo = wx.getStorageSync('plateNo')
    let vehicleType = wx.getStorageSync('vehicleType')
    if(vehicleId){
      this.setData({
        ['vehicleInfo.value1']: plateNo || "",
        ['vehicleInfo.value2']: vehicleType || "",
        ['vehicleInfo.url']:`/pages/select-vehicle/select-vehicle?id=${vehicleId}&name=${plateNo}&vehicleType=${vehicleType}`,
        ['submitInfo.vehicleId']: vehicleId
      })
    }
  },

  handleInfoChange(e){
    console.log("e>>>", e)
    let vehicleId = wx.getStorageSync('vehicleId')
    let vehicleResInfo = e.detail
    this.setData({
      vehicleAllInfo: vehicleResInfo || {},
      ['vehicleInfo.value1']: vehicleResInfo.plateNo || "",
      ['vehicleInfo.value2']: vehicleResInfo.vehicleType || "",
      ['vehicleInfo.url']:`/pages/select-vehicle/select-vehicle?id=${vehicleResInfo.id}&name=${vehicleResInfo.plateNo}&vehicleType=${vehicleResInfo.vehicleType}`,
      ['submitInfo.vehicleId']: vehicleResInfo.id
    })
    if(!vehicleId){
      wx.setStorageSync('vehicleId', vehicleResInfo.id)
      wx.setStorageSync('plateNo', vehicleResInfo.plateNo)
      wx.setStorageSync('vehicleType', vehicleResInfo.vehicleType)
      wx.setStorageSync('vin', vehicleResInfo.vin)
    }
    if(vehicleId != vehicleResInfo.id){
      wx.setStorageSync('vehicleId', vehicleResInfo.id)
      wx.setStorageSync('plateNo', vehicleResInfo.plateNo)
      wx.setStorageSync('vehicleType', vehicleResInfo.vehicleType)
      wx.setStorageSync('vin', vehicleResInfo.vin)
      let spaceId = wx.getStorageSync('spaceId')
      let startSoc = wx.getStorageSync('charging-init-start-soc')
      let queryFromScan = wx.getStorageSync('queryFromScan')
      if(!queryFromScan && spaceId){
        wx.removeStorageSync(`spaceId`)
      }
      if(startSoc){
        wx.removeStorageSync(`charging-init-start-soc`)
      }
    }
  },

  submitInfo(){
    if(!this.data.submitInfo.vehicleId){
      this.setData({
        showDialogAlert: true,
        dialogMsg:"请先选择车辆"
      })
    } else {
      let vehicleId = this.data.submitInfo.vehicleId
      let queryFromScan = wx.getStorageSync('queryFromScan')
      if(queryFromScan){
        wx.removeStorageSync(`queryFromScan`)
      }
      wx.navigateTo({
        url: `/pages/inspect-warning/inspect-warning?vehicleId=${vehicleId}`,
      })
    }
  }
  // handleScan(){
  //   const _this = this
  //   wx.scanCode({
  //     success (res) {
  //       let spaceId = res.result
  //       console.log("spaceId>>>", spaceId)
  //       _this.handleSelfDetectInfo(spaceId)
  //       // url = `/pages/inspect-charging/inspect-charging?spaceId=${spaceId}`
  //       // wx.navigateTo({
  //       //   url,
  //       // })
  //     }
  //   })
  // },

  // async handleSelfDetectInfo(code){
  //   let res = await selfDetectInfo({code})
  //   if(res.code === 200){
  //     let url = `/pages/inspect-charging/inspect-charging?spaceId=${code}`
  //     wx.navigateTo({
  //       url,
  //     })
  //   }
  // }
})